﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using skhukb.BL;
using skhukb.DB;
using skhukb.Models;
using System.Data.Linq;

namespace skhukb.Controllers
{
    [Authorize]
    public class BookmarkController : Controller
    {

        private void 기본그룹확인(DBDataContext db, int 사용자ID)
        {
            var 기본그룹 = db.책갈피그룹.FirstOrDefault(p => p.제목 == Consts.기본그룹 && p.사용자ID == 사용자ID);
            if (기본그룹 == null) {
                기본그룹 = new 책갈피그룹 { 사용자ID = 사용자ID, 제목 = Consts.기본그룹 };
                db.책갈피그룹.InsertOnSubmit(기본그룹);
                db.SubmitChanges();
            }
        }

        [HttpGet]
        public ActionResult GroupList()
        {
            using (var db = new DBDataContext()) {
                int 사용자ID = 현재사용자.사용자.ID;
                기본그룹확인(db, 사용자ID);
                return PartialView("_GroupList", new BookmarkGroupListModel { 책갈피그룹목록 = db.책갈피그룹.Where(p => p.사용자ID == 사용자ID).ToList() });
            }
        }

        [skhukb.Util.AjaxOnly] [HttpPost]
        public ActionResult Add(long subjectId, int articleCode, int bookmarkGroupId)
        {
            using (var db = new DBDataContext()) {
                var bookmark = new 책갈피 { 주제ID = subjectId, 글코드 = articleCode, 날짜 = DateTime.Now, 책갈피그룹ID = bookmarkGroupId };
                db.책갈피.InsertOnSubmit(bookmark);
                db.SubmitChanges();
                return Json(new JsonModel { success = true });

            }
        }

        [HttpGet]
        public ActionResult List()
        {
            using (var db = new DBDataContext()) {
                var 모델 = new BookmarkListModel() { 책갈피그룹목록 = new List<책갈피그룹Model>() };
                foreach (var 그룹 in db.책갈피그룹.Where(p => p.사용자ID == 현재사용자.사용자.ID)) {
                    var 그룹모델 = new 책갈피그룹Model(그룹);
                    그룹모델.책갈피목록 = 그룹.책갈피.Select(p => new 책갈피Model(p, Url)).ToList();
                    모델.책갈피그룹목록.Add(그룹모델);
                }
                return View(모델);
            }
        }

        [skhukb.Util.AjaxOnly] [HttpPost]
        public ActionResult AddGroup(string title)
        {
            using (var db = new DBDataContext()) {
                var 그룹 = new 책갈피그룹 { 사용자ID = 현재사용자.사용자.ID, 제목 = title };
                db.책갈피그룹.InsertOnSubmit(그룹);
                db.SubmitChanges();
                return Json(new JsonModel { success = true, url = Url.BookmarkList() });
            }
        }

        [skhukb.Util.AjaxOnly] [HttpPost]
        public ActionResult DeleteGroup(int id)
        {
            using (var db = new DBDataContext()) {
                var n = db.책갈피그룹.FirstOrDefault(p => p.ID == id);
                if (n != null) {
                    db.책갈피.DeleteAllOnSubmit(n.책갈피);
                    db.책갈피그룹.DeleteOnSubmit(n);
                }
                db.SubmitChanges();
                return Json(new JsonModel { success = true, url = Url.BookmarkList() });
            }
        }

        [skhukb.Util.AjaxOnly] [HttpPost]
        public ActionResult Delete(int id)
        {
            using (var db = new DBDataContext()) {
                var n = db.책갈피.FirstOrDefault(p => p.ID == id);
                if (n != null) db.책갈피.DeleteOnSubmit(n);
                db.SubmitChanges();
                return Json(new JsonModel { success = true, url = Url.BookmarkList() });
            }
        }

        [skhukb.Util.AjaxOnly] [HttpPost]
        public ActionResult RenameGroup(int id, string title)
        {
            using (var db = new DBDataContext()) {
                var n = db.책갈피그룹.FirstOrDefault(p => p.ID == id);
                if (n == null) return Json(new JsonModel { success = false, message = Consts.책갈피그룹없음 });
                n.제목 = title;
                db.SubmitChanges();
                return Json(new JsonModel { success = true, url = Url.BookmarkList() });
            }
        }

        [skhukb.Util.AjaxOnly] [HttpPost]
        public ActionResult Move(int bookmarkId, int bookmarkGroupID)
        {
            using (var db = new DBDataContext()) {
                var n = db.책갈피.FirstOrDefault(p => p.ID == bookmarkId);
                if (n == null) return Json(new JsonModel { success = false, message = Consts.책갈피없음 });
                n.책갈피그룹ID = bookmarkGroupID;
                db.SubmitChanges();
                return Json(new JsonModel { success = true, url = Url.BookmarkList() });
            }
        }

    }
}
